package com.whfc.openapi.api.car.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.util.ImageUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.openapi.api.car.service.OpenCarDataService;
import com.whfc.uni.dto.CarInoutRecordDTO;
import com.whfc.uni.dto.CarInoutRecordSyncDTO;
import com.whfc.uni.enums.Direction;
import com.whfc.uni.service.CarInoutRecordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;

/**
 * @Description 车辆管理
 * @Author hw
 * @Date 2020/12/15 15:31
 * @Version 1.0
 */
@Service
public class OpenCarDataServiceImpl implements OpenCarDataService {

    private final Logger logger = LoggerFactory.getLogger(this.toString());

    private static final String IN = "in";

    private static final String OUT = "out";

    @DubboReference(interfaceClass = CarInoutRecordService.class, version = "1.0.0")
    private CarInoutRecordService carInoutRecordService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public void insertRecord(CarInoutRecordSyncDTO inOutRecordSyncDTO, MultipartFile[] files) {
        String inImgName = "";
        String outImgName = "";
        String filePrefix = filePathConfig.getFilePath("car/plate/img");
        //上传图片
        for (MultipartFile file : files) {
            String fileName = RandomUtil.getRandomFileName() + ".jpg";
            try {
                //ms/dev/car/plate/img
                String imgUrl = fileHandler.upload(filePrefix + "/" + fileName, file.getInputStream());
                String originalFilename = file.getOriginalFilename();
                if (originalFilename != null) {
                    if (originalFilename.contains(IN)) {
                        inImgName = imgUrl;
                    } else if (originalFilename.contains(OUT)) {
                        outImgName = imgUrl;
                    }
                }
            } catch (Exception e) {
                logger.warn("上传图片失败", e);
            }
        }
        CarInoutRecordDTO carInRecord = new CarInoutRecordDTO();
        carInRecord.setDeptId(inOutRecordSyncDTO.getDeptId());
        carInRecord.setName(inOutRecordSyncDTO.getName());
        carInRecord.setLicensePlate(inOutRecordSyncDTO.getPlate());
        carInRecord.setDeviceName(inOutRecordSyncDTO.getEnterName());
        carInRecord.setDirection(Direction.IN.getValue());
        carInRecord.setTime(inOutRecordSyncDTO.getEnterTime());
        carInRecord.setImgUrl(inImgName);
        carInoutRecordService.saveRecord(carInRecord);
        if (inOutRecordSyncDTO.getOutTime() != null && StringUtils.isNotEmpty(inOutRecordSyncDTO.getOutName())) {
            //保存出去记录
            CarInoutRecordDTO carOutRecord = new CarInoutRecordDTO();
            carOutRecord.setDeptId(inOutRecordSyncDTO.getDeptId());
            carOutRecord.setName(inOutRecordSyncDTO.getName());
            carOutRecord.setLicensePlate(inOutRecordSyncDTO.getPlate());
            carOutRecord.setDeviceName(inOutRecordSyncDTO.getOutName());
            carOutRecord.setDirection(Direction.OUT.getValue());
            carOutRecord.setTime(inOutRecordSyncDTO.getOutTime());
            carOutRecord.setImgUrl(outImgName);
            carInoutRecordService.saveRecord(carOutRecord);
        }
    }

    @Override
    public void recordSync(Integer deptId, String deviceName, String name, String licensePlate, Date time, Integer direction, InputStream inputStream) {

        //图片上传
        String imgUrl = null;
        if (inputStream != null) {
            try {
                String fileName = RandomUtil.getRandomFileName() + ".png";
                String ossKey = filePathConfig.getFilePath("car/plate/img/", fileName);
                imgUrl = fileHandler.upload(ossKey, inputStream);
            } catch (Exception e) {
                logger.warn("上传图片失败", e);
            }
        }

        CarInoutRecordDTO record = new CarInoutRecordDTO();
        record.setDeptId(deptId);
        record.setName(StringUtils.isNotBlank(name) ? name : "临时车辆");
        record.setLicensePlate(licensePlate);
        record.setDeviceName(deviceName);
        record.setDirection(direction);
        record.setTime(time);
        record.setImgUrl(imgUrl);
        carInoutRecordService.saveRecord(record);
    }

    @Override
    public void recordSync(Integer deptId, String deviceName, String name, String licensePlate, Date time, Integer direction, String imgBase64) {

        //图片上传
        InputStream inputStream = null;
        //base64图片解码
        if (StringUtils.isNotBlank(imgBase64) && imgBase64.length() > 1024) {
            try {
                byte[] imageBytes = ImageUtil.base64ToImage(imgBase64);
                inputStream = new ByteArrayInputStream(imageBytes);
            } catch (Exception ex) {
                logger.warn("图片转换识别", ex);
            }

        }

        this.recordSync(deptId, deviceName, name, licensePlate, time, direction, inputStream);
    }
}
